
/* USART */

#ifndef 	__USART_H_
#define 	__USART_H_

extern unsigned char PortSelect;

#define 	 MHz			*1000000UL
#define   FOSC 		40MHz

#define 	 BAUD 		9600 
     
#if defined(PRINTER__POS58III) || defined(APP_COMMAND_TRANSLATOR)
#define   BAUD_2  	9600
#else
#define   BAUD_2  	115200
#endif

#define   B_NINE 		0     /* Use 9bit communication? FALSE=8bit */
#define   B_NINE_2	0

/* Flow control - active level - LOW */
#define   CTS			RC1			  /* Input. If CTS in low level, then modem is ready to accept data  */
#define   RTS			RC5			  /* Output. If RTS is set(high level) it's say to modem - "NOT to send data" */

#define   CTS_DIR		TRISC1
#define   RTS_DIR		TRISC5


/* USART 1 */
#if B_NINE == 1
 #define NINE_BITS 0x40
#else
 #define NINE_BITS 0
#endif

#if BAUD > 9600
 #define HIGH_SPEED 1
#else
 #define HIGH_SPEED 0
#endif

#if HIGH_SPEED == 1
 #define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
 #define SPEED 	0x4
#else
 #define DIVIDER ((int)(FOSC/(64UL * BAUD) -1))	
 #define SPEED 	0x00
#endif

#define 	RX_PIN 		TRISC7
#define 	TX_PIN 		TRISC6

/* Serial_1  initialization */
#define USART_Init()\
	RX_PIN = 1;	\
	TX_PIN = 0;		  \
   CTS_DIR = 1; \
   RTS_DIR = 0; \
 	SPBRG1 = DIVIDER;     	\
	RCSTA1 = (NINE_BITS|0x90);	\
	TXSTA1 = (SPEED|NINE_BITS|0x20); NOP(); NOP(); RTS = 0; 
/* End of USART 1 */

/* USART 2 */
#if B_NINE_2 == 1
 #define NINE_BITS_2 	0x40
#else
 #define NINE_BITS_2 	0
#endif

#if BAUD_2 > 9600
 #define HIGH_SPEED_2	1
#else
 #define HIGH_SPEED_2 	0
#endif

#if HIGH_SPEED_2 == 1
 #define DIVIDER_2 ((int)(FOSC/(16UL * BAUD_2) -1))
 #define SPEED_2 	0x4
#else
 #define DIVIDER_2 ((int)(FOSC/(64UL * BAUD_2) -1))	
 #define SPEED_2 	0x00
#endif

#define  RX2_PIN		TRISG2	 
#define  TX2_PIN		TRISG1

/* Serial_2  initialization */
#define USART_2_Init()\
	RX2_PIN = 1;	\
	TX2_PIN = 0;		  \
	SPBRG2 = DIVIDER_2;     	\
	RCSTA2 = (NINE_BITS_2|0x90);	\
	TXSTA2 = (SPEED_2|NINE_BITS_2|0x20)
/* End of USART 2 */


void puts ( unsigned char*, unsigned char Port );

// put a string with define string length
void putds ( unsigned char *s, unsigned int length, unsigned char Port );		

void putsf( const unsigned char *s, unsigned char Port );

void putch( unsigned char, unsigned char Port);
unsigned char getch( unsigned char Port);
unsigned char getche( unsigned char Port );		  

#endif
